import Taro from "@tarojs/taro";
import { useEffect, useState } from "react";
import { View, Button, Text, Radio } from "@tarojs/components";

import "./index.scss";
import request from "../../utils/request";
const shipping = () => {
  const [addrs, setaddrs] = useState([]);
  const [numflag, setnumflag] = useState(0);
  useEffect(() => {
    // 获取地址列表
    request("/addr/list", "get").then((res) => {
      console.log(res);
      setaddrs(res.data.list);
    });
    return () => {};
  }, [numflag]);
  const operation = (id) => {
    request("/addr/del", "post", { _id: id }).then((res) => {
      // console.log(res);
      Taro.showToast({
        title: res.msg,
        duration: 2000,
        success() {
          setnumflag(numflag + 1);
        },
      });
    });
  };
  const editAddr=(ele)=>{
    let str = JSON.stringify(ele)
    Taro.navigateTo({
      url:'../address/address?ele='+str
    })
  }
  const randerAddrList = () => {
    return (
      addrs.length > 0 &&
      addrs.map((item) => (
        <View className="ship_item" key={item._id}>
          <View className="item_top">
            <Text className="name">{item.name}</Text>
            <Text className="tel">{item.tel}</Text>
          </View>
          <View className="addr">
            {item.region[0] + item.region[1] + item.region[2] + item.detailaddr}
          </View>
          <View className="elit">
            <Radio>设为默认</Radio>
            <View className="elit_cz">
              <Text className="cz1" onClick={() => editAddr(item)}>
                编辑
              </Text>
              <Text className="cz2" onClick={() => operation(item._id)}>
                删除
              </Text>
            </View>
          </View>
        </View>
      ))
    );
  };
  return (
    <View className="shipping">
      {randerAddrList()}
      <Button
        className="addaddr"
        onClick={() => Taro.navigateTo({ url: "../address/address" })}
      >
        添加地址
      </Button>
    </View>
  );
};
export default shipping;
